<template>
  <div class="page-container">
    <el-form class="container" ref="formRef" :rules="rules" :model="form" @submit.prevent="submitForm(formRef)">
      <h1>个人中心</h1>
      <el-form-item class="ipt-box" prop="oldPassword">
        <el-input v-model="form.oldPassword" class="input" type="password" placeholder="旧密码" clearable
                  show-password/>
      </el-form-item>
      <el-form-item class="ipt-box" prop="password">
        <el-input v-model="form.password" class="input" type="password" placeholder="新密码" clearable show-password/>
      </el-form-item>
      <el-form-item class="ipt-box" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" class="input" type="password" placeholder="确认密码" clearable
                  show-password/>
      </el-form-item>
      <el-button :bg="true" :loading="isLoading" type="primary" class="login-button" native-type="submit">提交
      </el-button>
    </el-form>
    <div class="footer">
      <a href="https://beian.miit.gov.cn/" target="_blank">
        <span class="footer-left-item-text">浙ICP备2021035852号-1</span>
      </a>
      <span class="footer-left-item-text">Copyright © 2025 FuX</span>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from 'vue';
import md5 from 'js-md5';
import {useRouter} from 'vue-router';
import {ElMessage} from 'element-plus';
import request from '@/api/sys/AccountApi';

const router = useRouter();
const isLoading = ref(false);
const formRef = ref('');
const form = reactive({
  oldPassword: '',
  password: '',
  confirmPassword: ''
});

const submitForm = (formRef) => {
  formRef.validate((valid) => {
    if (valid) {
      isLoading.value = true;
      request.changePassword({
        oldPassword: md5.md5(form.oldPassword),
        newPassword: md5.md5(form.password)
      }).then(() => {
        localStorage.clear();
        ElMessage.success({message: '操作成功'});
        router.replace({name: 'login'});
      }).finally(() => {
        isLoading.value = false;
      });
    }
  });
};

const rules = {
  oldPassword: [
    {
      validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输旧密码'));
        } else {
          callback();
        }
      }, trigger: 'blur'
    }
  ],
  password: [
    {
      validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          callback();
        }
      }, trigger: 'blur'
    }
  ],
  confirmPassword: [
    {
      validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请确认密码'));
        } else {
          if (form.password !== value) {
            callback(new Error('两次密码不一致'));
          } else {
            callback();
          }
        }
      }, trigger: 'blur'
    }
  ]
}
</script>

<style scoped>
.page-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
  background-size: 100% 100%;
  background-image: url("https://zchuan-miy.oss-cn-hangzhou.aliyuncs.com/scene-dir/2025-06-30/1751284632326.jpg");
}

.footer {
  position: fixed;
  bottom: 10px;
  width: 100%;
  height: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.footer a {
  text-decoration: none;
}

.footer-left-item-text {
  font-size: 14px;
  color: #999;
}

.container {
  position: relative;
  z-index: 1;
  width: 500px;
  height: 450px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 3px;
  transition: all 500ms;
}

.container:hover {
  transform: scale(1.10);
  border-radius: 20px;
  box-shadow: 0 8px 50px rgba(0, 0, 0, 0.2);
}

h1 {
  font-size: 30px;
  width: 75%;
  letter-spacing: 10px;
  margin-bottom: 30px;
}

.ipt-box {
  width: 75%;
  margin: 10px 0;
  border-radius: 5px;
  position: relative;
  z-index: 2;
}

.el-input {
  --el-input-inner-height: 40px;
}

.login-button {
  width: 200px;
  height: 50px;
  margin-left: 175px;
  margin-top: 20px;
  border: none;
  background: linear-gradient(-200deg, #fac0e7, #aac2ee);
  color: #fff;
  font-weight: bold;
  font-size: 18px;
  letter-spacing: 8px;
  border-radius: 10px;
  cursor: pointer;
  transition: 0.5s;
}

.login-button:hover {
  background: linear-gradient(-200deg, #aac2ee, #fac0e7);
  background-position-x: -200px;
}

</style>
